package com.ripplezjw.blog.controller;

import com.ripplezjw.blog.Service.ArticleService;
import com.ripplezjw.blog.Service.ClassificationService;
import com.ripplezjw.blog.Service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class NavController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ClassificationService classificationService;
    @Autowired
    TagService tagService;

    @Autowired
    SimpleDateFormat simpleDateFormat;

    @GetMapping({"/", "/index"})
    public String index(Model model) {
        model.addAttribute("cur_page", "index");
        model.addAttribute("articles", articleService.showAllArticle());
        return "index";
    }

    @GetMapping("/time")
    public String time(Model model) {
        model.addAttribute("cur_page", "time");
        List<Date> createTimes = articleService.getCreateTime();

        model.addAttribute("createTimes", articleService.getCreateTime()
                .stream()
                .map(simpleDateFormat::format)
                .collect(Collectors.toList()));
        model.addAttribute("modTimes", articleService.getModTime()
                .stream()
                .map(simpleDateFormat::format)
                .collect(Collectors.toList()));
        return "time";
    }

    @GetMapping("/classification")
    public String classification(Model model) {
        model.addAttribute("cur_page", "classification");
        model.addAttribute("clss", classificationService.showAllCls());
        return "classification";
    }

    @GetMapping("/tag")
    public String tag(Model model) {
        model.addAttribute("cur_page", "tag");
        model.addAttribute("tags", tagService.showAllTags());
        return "tag";
    }
}
